
#!/bin/bash

# 进入脚本所在目录（动态获取路径）
BASE_DIR=$(dirname "$(realpath "$0")")
cd "$BASE_DIR" || { echo "目录切换失败"; exit 1; }

# 加载环境
. /opt/ros/humble/setup.bash || { echo "ROS环境加载失败"; exit 1; }
. ~/Desktop/RCIA_Vision/install/setup.bash || { echo "项目环境加载失败"; exit 1; }

# 显示环境信息
echo "当前工作目录: $(pwd)"
echo "ROS版本: $(ros2 --version)"
echo "构建类型: Release"

# 清理旧构建
echo "清理旧构建目录..."
rm -rf build install log || { echo "清理失败"; exit 1; }

# 动态设置并行构建
NPROC=$(nproc)
echo "使用 $NPROC 个并行工作线程"

# 启用IPP优化（需确保OpenCV已配置IPP）
echo "构建目标包: vision_interfaces, vision_guard, vision_detector"
colcon build \
  --packages-select vision_interfaces vision_guard vision_detector \
  --cmake-args -DCMAKE_BUILD_TYPE=Release \
  --parallel-workers $NPROC  #> build.log 2>&1

# 检查构建结果
if [ $? -eq 0 ]; then
  echo "构建成功！日志路径: $BASE_DIR/build.log"
else
  echo "构建失败！请检查 $BASE_DIR/build.log"
  exit 1
fi